package safari.model.free;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import safari.model.common.FreeListVO;
import safari.model.common.ListVO;
import safari.model.common.PagingBean;
import safari.model.common.SearchVO;

public class FreeBoardServiceImpl implements FreeBoardService {
	private FreeBoardDao fd;
	public FreeBoardServiceImpl(FreeBoardDao fd){
		this.fd = fd;
	}
	@Override
	public void freeBoardRegister(FreeBoardVO fvo) throws SQLException {
		fd.freeBoardRegister(fvo);
	}

	@Override
	public void freeBoardUpdate(FreeBoardVO fbvo) throws SQLException {
		fd.freeBoardUpdate(fbvo);
		
	}
	@Override
	public FreeBoardVO freeBoardUpdateView(int no) throws SQLException {
		return fd.freeBoardUpdateView(no);
	}

	@Override
	public void freeBoardDelete(int no) throws SQLException {
		fd.freeBoardDelete(no);
	}

	@Override
	public FreeListVO freeBoardShowContent(String boardNo, String pageNo) throws SQLException {
		int no = Integer.parseInt(boardNo);
		int comNo = 1;
		if(pageNo!=null){
			comNo = Integer.parseInt(pageNo);
		}
		fd.freeBoardUpdateHits(no);
		FreeBoardVO fbvo = fd.freeBoardShowContent(no);
		FreeBoardNoAndPage board = new FreeBoardNoAndPage(no, comNo);
		//게시물의 no와 comment의 comf 는 같다.
		List<FreeBoardCommentVO> list = fd.freeBoardGetAllComment(board);
		int totalContents = fd.freeBoardCommentGetTotalCount(no);
		PagingBean pb = new PagingBean(totalContents,comNo);
		return new FreeListVO(fbvo, list, pb);
	}

	@Override
	public void freeBoardUpdateHits(int no) throws SQLException {
		
	}

	@Override
	public ListVO freeBoardGetAllList(String page) throws SQLException {
		int pn=1;
		if(page != null){
			pn = Integer.parseInt(page);
		}
		List<FreeBoardVO> list = fd.freeBoardGetAllList(pn);
		int totalContents = fd.freeBoardGetTotalContentCount();
		System.out.println(totalContents);
		PagingBean pb = new PagingBean(totalContents, pn);
		
		return new ListVO(list, pb);
	}
	
	@Override
	public void freeBoardReplyRegister(FreeBoardVO fbvo) throws SQLException {
		fd.freeBoardReplyRegister(fbvo);
		fbvo = fd.freeBoardShowContent(fbvo.getNo());
		fd.freeBoardReplyUpdate(fbvo);
		
	}
	@Override
	public FreeBoardVO freeBoardReplyView(String paramNo) throws SQLException {
		int no = Integer.parseInt(paramNo);
		return fd.freeBoardReplyView(no);
	}

	@Override
	public List<FreeBoardCommentVO> freeBoardCommentRegisterAndShowComment(FreeBoardCommentVO fbcVO) throws SQLException {
		fd.freeBoardCommentRegister(fbcVO);
		//등록과 동시에 페이지처리 기본 1페이지를 보여준다.
		FreeBoardNoAndPage fbnap = new FreeBoardNoAndPage();
		fbnap.setPage(1);
		return fd.freeBoardGetAllComment(fbnap);
	}
	@Override
	public FreeBoardCommentVO freeBoardCommentUpdateView(int no)
			throws SQLException {
		return fd.freeBoardCommentUpdateView(no);
	}
	@Override
	public void freeBoardCommentUpdate(FreeBoardCommentVO fbcVO)
			throws SQLException {
		fd.freeBoardCommentUpdate(fbcVO);
		
	}
	@Override
	public List<FreeBoardCommentVO> freeBoardCommentGetAllList(String page) throws SQLException {
		int pageNo=1;
		if(page!=null){
			pageNo=Integer.parseInt(page);
		}
		FreeBoardNoAndPage fbnap = new FreeBoardNoAndPage();
		fbnap.setPage(pageNo);
		return fd.freeBoardGetAllComment(fbnap);
	}



	@Override
	public void freeBoardCommentDelete(int no) throws SQLException {
		fd.freeBoardCommentDelete(no);
		
	}

	@Override
	public ListVO freeBoardSearchKeyword(SearchVO svo)throws SQLException {
		List<FreeBoardVO> list = fd.freeBoardSearchKeyword(svo);
		int totalContents = fd.freeBoardSearchKeywordCount(svo);
		System.out.println("total : " + totalContents);
		PagingBean pb = new PagingBean(totalContents,1);
		System.out.println(pb.getStartPageOfPageGroup());
		System.out.println(pb.getEndPageOfPageGroup());
		return new ListVO(list, pb);
	}



}
